package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-manage/cache"
	"logi-manage/modle"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	carNo := c.PostForm("carNo")     //汽车编号
	carCode := c.PostForm("carCode") //车牌号
	carName := c.PostForm("carName") //车的名字

	var caradd = modle.Car{
		CarNo:   carNo,
		CarCode: carCode,
		CarName: carName,
	}

	err := modle.DB.Debug().Create(&caradd).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return

	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": caradd,
	})
	return

}

func CarOrderAdd(c *gin.Context) {
	orderNo := c.PostForm("orderNo")     //汽车编号
	orderForm := c.PostForm("orderForm") //车牌号
	orderTo := c.PostForm("orderTo")     //车的名字
	orderGood := c.PostForm("orderGood")
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	var data = modle.CarOrder{
		OrderNo:   orderNo,
		OrderForm: orderForm,
		OrderTo:   orderTo,
		OrderGood: orderGood,
		Cid:       cid,
	}

	err := modle.DB.Debug().Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return

	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": data,
	})
	return

}

func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size

	var carOrderList []modle.CarOrderList
	/*
		SELECT
		car_orders.id AS OrderId,
		car_orders.order_form AS OrderForm,
		car_orders.order_good AS OrderGood,
		car_orders.order_no AS OrderNo,
		car_orders.order_to AS OrderTo,
		cars.car_code AS CarCode,
		cars.car_name AS CarName,
		cars.car_no AS CarNo
		 FROM
		 car_orders
		LEFT JOIN cars on cars.id=car_orders.cid

	*/

	redis_key := "car_order_list_page" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		modle.DB.
			Table("car_orders").
			Select("car_orders.id AS OrderId",
				"car_orders.order_form AS OrderForm",
				"car_orders.order_good AS OrderGood",
				"car_orders.order_no AS OrderNo",
				"car_orders.order_to AS OrderTo",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_no AS CarNo").
			Joins("LEFT JOIN cars on cars.id=car_orders.cid").
			Debug().
			Limit(size).
			Offset(offset).
			Find(&carOrderList)

		jsonData, _ := json.Marshal(carOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrderList)
	}

	var total int64
	modle.DB.Model(&modle.CarOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "success",
		"data":  carOrderList,
		"count": total,
	})
	return

}

func CarOrderDetail(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("id"))

	var carOrderDetail []modle.CarOrderList
	/*
		SELECT
		car_orders.id AS OrderId,
		car_orders.order_form AS OrderForm,
		car_orders.order_good AS OrderGood,
		car_orders.order_no AS OrderNo,
		car_orders.order_to AS OrderTo,
		cars.car_code AS CarCode,
		cars.car_name AS CarName,
		cars.car_no AS CarNo
		 FROM
		 car_orders
		LEFT JOIN cars on cars.id=car_orders.cid

	*/

	redis_key := "car_order_detail_id" + strconv.Itoa(id)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		modle.DB.
			Table("car_orders").
			Select("car_orders.id AS OrderId",
				"car_orders.order_form AS OrderForm",
				"car_orders.order_good AS OrderGood",
				"car_orders.order_no AS OrderNo",
				"car_orders.order_to AS OrderTo",
				"cars.car_code AS CarCode",
				"cars.car_name AS CarName",
				"cars.car_no AS CarNo").
			Joins("LEFT JOIN cars on cars.id=car_orders.cid").
			Debug().
			Where("car_orders.id=?", id).
			Find(&carOrderDetail)

		jsonData, _ := json.Marshal(carOrderDetail)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrderDetail)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": carOrderDetail,
	})
	return
}
